//
//  AppDelegate.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/19.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
        
        
        
        //(1)初始化window
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //(2)设置window的颜色
        self.window?.backgroundColor = UIColor.whiteColor()
        
        //(3)设置根控制器
        //根据登录情况来设置首先进入到哪个界面
        setupDefaultRootVc()
        //self.window?.rootViewController = MainController()
        
        //(4)显示主窗口
        self.window?.makeKeyAndVisible()
        
        self.setUpCommonUI()
        
        
        return true
    }
    
    //通过登录情况来设置进入哪个界面
    func setupDefaultRootVc()
    {
        //1-首先通过AuthViewModel单例类从归档中获取登录信息
        //2-根据登录信息来判断
        //3-如果已经登录了
        //4-如果已经登录了，再判断当前的版本是不是最新的版本
        //5-如果app当前的版本已经是最新的版本了，就先进入新特性界面，再进入欢迎界面
        //6-如果不是最新的版本，就直接进入欢迎界面
        if AuthViewModel.AuthViewModelTools.isLogin
        {
            if !isNewVersion()
            {
                self.window?.rootViewController = WelocomeController()
            }
            else
            {
                self.window?.rootViewController = NewFeatureController()
            }
        }
        //如果从未登陆过(先进入未登录的主界面)
        else
        {
            self.window?.rootViewController = MainController()
        }
    }
    
    
    //设置一些最基本的信息
    func setUpCommonUI()
    {
        //设置navaigation Bar的字体主色调为主题颜色
        //UINavigationBar.appearance().tintColor = THEMEColor
        
        //设置为自定义模式
        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.Custom)
        //设置背景颜色
        SVProgressHUD.setBackgroundColor(THEMEColor)
        //设置前面的颜色
        SVProgressHUD.setForegroundColor(UIColor.whiteColor())
    }
    
    //判断是不是最新的版本
    func isNewVersion() -> Bool
    {
        
        //(1)获取当前APP的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        // 把字符串转成double类型
        let currentVersionD = Double(currentVersion)!
        //(2)获取偏好中保存的APP版本(旧的版本信息)
        let shAppVersion = "shAppVersion"
        let oldVersion = NSUserDefaults.standardUserDefaults().doubleForKey(shAppVersion)
        //(3)保存获取到的当前版本
       NSUserDefaults.standardUserDefaults().setDouble(currentVersionD, forKey: shAppVersion)
        //(4)返回是否是最新版本
        return currentVersionD > oldVersion
        //return true
    }
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    
    //在应用进入后台之后清理缓存效果最好!!!!!!!
    func applicationDidEnterBackground(application: UIApplication)
    {
        
        
        DataBaseViewModel.ClearLocaleCache()
       
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

